﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace AAA.Database.Loader
{
    public class DatabaseLoader : ILoader
    {
        private IDatabase _database;
        private bool _isDeleteRecord;

        protected IDatabase CreateDatabase(string strDllName, string strClassName)
        {
            Assembly asmb;
            
            try
            {
                asmb = Assembly.LoadFrom(strDllName);
                return (IDatabase)asmb.CreateInstance(strClassName);    
            }
            catch (Exception ex)
            {
            }
            return null;
        }

        protected string CreateStatement(string strSqlStatement, Dictionary<string, string> dicData)
        {
            try
            {
                int iStartIndex;
                int iEndIndex;
                string strStmt = strSqlStatement;
                string strKey;
                string strValue;

                iEndIndex = -1;
                while ((iStartIndex = strSqlStatement.IndexOf('{', iEndIndex + 1)) > -1)
                {
                    iEndIndex = strSqlStatement.IndexOf('}', iStartIndex + 1);

                    if (iEndIndex < 0)
                        return strStmt;

                    strKey = strStmt.Substring(iStartIndex + 1, iEndIndex - iStartIndex - 1);

                    if (dicData.ContainsKey(strKey) == false)
                        continue;
                    


                }
            }
            catch (Exception ex)
            {
            }
            return null;
        }

        protected virtual string SqlStatement(string strSqlStatement)
        {
            return null;
        }

        #region ILoader Members

        public bool IsDeleteRecord
        {
            get { return _isDeleteRecord; }
            set { _isDeleteRecord = value; }
        }

        public bool Load(Dictionary<string, string> dicConfig)
        {
            string strStmt;
            string strSqlStatement;

            try
            {
                _database = CreateDatabase(dicConfig["dll"], dicConfig["class-name"]);

                if (_database == null)
                    return false;

                if (IsDeleteRecord)
                {
                    strSqlStatement = dicConfig["delete-sql"];

                    if ((strStmt = CreateStatement(strSqlStatement, dicConfig)) != null)
                        _database.ExecuteUpdate(strStmt);
                }

                strSqlStatement = CreateStatement(dicConfig["update-sql"], dicConfig);

                while ((strStmt = SqlStatement(strSqlStatement)) != null)
                {
                    _database.ExecuteUpdate(strStmt);
                }

                return true;
            }
            catch (Exception ex)
            {

            }
            return false;
        }

        #endregion
    }
}
